package com.thon.controller.rest;

import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.dao.util.Page;
import com.thon.entity.project.Preferential;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectShare;
import com.thon.service.project.PreferentialService;
import com.thon.service.project.ProjectService;
import com.thon.service.project.ProjectShareService;

/**
 * @file ProjectShareRestCountroller.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月18日 上午11:35:48
 * @description: TODO
 */
@Controller
@RequestMapping(value = "/api/project/share")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ProjectShareRestController {

	private static final Log log = LogFactory.getLog(ProjectShareRestController.class);
	@Autowired
	private ProjectService projectService;
	@Autowired
	private PreferentialService preferentialService;
	@Autowired
	private ProjectShareService projectShareService;

	// 根据项目id添加分享并更新project中的分享数
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> shareProject(
			@Valid @RequestBody ProjectShare projectShare) {

		projectShareService.add(projectShare);

		Project project = projectShare.getProject();
		Preferential preferntial = projectShare.getPreferential();
		if(project!=null)
		{		
		int projectId = project.getId();
		project = projectService.getProject(-1,projectId);
		Integer shareCount = projectShareService.countProjectShare(projectId,0).intValue();

		project.setShareCount(shareCount);

		projectService.updateProject(project);

		return new ResponseEntity(true,HttpStatus.CREATED);
	}
	else if(preferntial!=null)
	{
		int preferntialId = preferntial.getId();
		preferntial = preferentialService.getPreferential(preferntialId);
		Integer shareCount = projectShareService.countProjectShare(0,preferntialId).intValue();
		preferntial.setShareCount(shareCount);
		preferentialService.updatePreferential(preferntial);
		return new ResponseEntity(true,HttpStatus.CREATED);
	}
	else		

		return new ResponseEntity(false,HttpStatus.CREATED);
	}

	// 根据项目分享id获取项目分享
	@RequestMapping(value = "/getShare", method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> getProjectAttention(
			@RequestParam(value = "id", required = true) int id, Model model) {

		ProjectShare projectShare = projectShareService
				.getProjectShare(id);
		model.addAttribute("projectShare", projectShare);
		return new ResponseEntity(model, HttpStatus.OK);
	}
	//根据用户id获取分享列表
	@RequestMapping(value="/user/list",method=RequestMethod.GET)
	public ResponseEntity<HttpEntity> getProjectShares(
			@RequestParam(value="uid") Integer userId,
			@RequestParam(value="method",defaultValue="0") Integer method,
			@RequestParam(value="p",defaultValue="1") Integer pageNo,
			@RequestParam(value="s",defaultValue="4") Integer pageSize){
		Page<ProjectShare> page=projectShareService.findProjectShareByUser(userId,method,pageNo,pageSize);
		return new ResponseEntity(page, HttpStatus.OK);
		
	}

}
